package org.jks.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.exception.ConstraintViolationException;
import org.jks.config.Config;
import org.jks.model.Position;
import org.jks.request.model.Ids;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.PositionService;
import org.jks.util.JsonUtil;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/position")
public class PositionController {
	@Autowired
	PositionService positionService;

	@RequestMapping("/add")
	public void add(Position position, HttpServletRequest request,
			HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		if (StringUtils.isEmpty(position.getName())) {
			ar.setMsg("职位名称不能为空");
		} else {
			try {
				positionService.save(position);
				ar.setSuccess(true);
				ar.setData(position);
			} catch (ConstraintViolationException e) {
				ar.setMsg("职位名称冲突");
			} catch (Exception e) {
				ar.setMsg("添加职位失败");
				e.printStackTrace();
			}
		}

		ResponseUtil.returnJson(response, ar);
	}

	@RequestMapping("/delete")
	public void delete(Ids ids, HttpServletRequest request,
			HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		try {
			positionService.delete(ids.getIds());
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setMsg("删除职位失败");
		}
		ResponseUtil.returnJson(response, ar);
	}

	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable long id, HttpServletRequest request,
			HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		try {
			positionService.delete(id);
			ar.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			ar.setMsg(e.getMessage());
		}
		ResponseUtil.returnJson(response, ar);
	}

	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable long id,
			HttpServletRequest request, HttpSession session,
			HttpServletResponse response, Map<String, Object> model) {
		try {
			Position position = positionService.findById(id);
			model.put("position", JsonUtil.Object2Json(position));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Config.init(request, response, session, model);
		return new ModelAndView("sysMgr/position_detail", model);
	}

	@RequestMapping("/update")
	public void update(Position position, HttpServletRequest request,
			HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		try {
			positionService.update(position);
			ar.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			ar.setMsg(e.getMessage());
		}
		ResponseUtil.returnJson(response, ar);
	}

	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		try {
			List<Position> positions = positionService.all();
			// PrintUtil.print(flights);
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setRows(positions);
			ar.setData(table);
		} catch (Exception e) {
			e.printStackTrace();
			ar.setMsg(e.getMessage());
		}
		ResponseUtil.returnJson(response, ar);
	}
}
